package com.universest.swordholder.ui.item

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Article
import androidx.compose.material.icons.filled.Book
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.universest.swordholder.SwordHolderApplication
import com.universest.swordholder.data.dao.Book
import com.universest.swordholder.ui.theme.SwordHolderTheme

@Composable
fun WorksItemWidget(modifier: Modifier, book: Book, description: String = "", searchKeyword: String = "",dropMenu: @Composable () -> Unit = {}) {
    // 定义统一的图片尺寸和形状
    val imageShape = RoundedCornerShape(8.dp)

    Row(modifier = modifier, verticalAlignment = Alignment.CenterVertically) {
        // 检查图片地址是否为空
        Box(modifier = Modifier
            .padding(start = 15.dp)
            .height(80.dp)
            .width(60.dp))
        {
            var pictureLoadError by remember { mutableStateOf(false) }
            if (book.bookImageSrc.isBlank() || pictureLoadError) {
                // 显示带边框的默认图标
                Icon(
                    imageVector = if(book.singlePassage)Icons.AutoMirrored.Default.Article else Icons.Default.Book,
                    contentDescription = "Book placeholder",
                    tint = MaterialTheme.colorScheme.primary,
                    modifier = Modifier
                        .fillMaxSize()
                        .border(2.dp, Color.Gray, imageShape)
                        .clip(imageShape)
                )
            } else {
                // 加载网络图片，失败时显示带边框的图标
                AsyncImage(
                    model = book.bookImageSrc,
                    contentDescription = "Book cover",
                    modifier = Modifier
                        .fillMaxSize()
                        .clip(imageShape),
                    onError =  {
                        pictureLoadError = true
                    },
                )
            }
        }

        Column(modifier = Modifier.padding(start = 15.dp)) {
            Text(
                modifier = Modifier.padding(5.dp),
                text = book.bookName,
                fontSize = 18.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.onSurface)
            dropMenu()
            Text(
                modifier = Modifier.padding(5.dp),
                text = description,
                fontSize = 14.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                color = MaterialTheme.colorScheme.onSurfaceVariant)
        }
    }
}

@Preview
@Composable
fun WorksItemWidgetPreview(){
    SwordHolderApplication.previewContext = LocalContext.current
    SwordHolderTheme {
        WorksItemWidget(modifier = Modifier.height(100.dp).width(600.dp), book = Book(
            bookName = "书名",
            charCount = 114514,
            bookDescription = "描述",
        ))
    }
}